package com.lxd.sunso.product.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxd.sunso.common.utils.*;
import com.lxd.sunso.product.components.BCryptPasswordEncoderUtil;
import com.lxd.sunso.product.components.ConversionUtil;
import com.lxd.sunso.product.entity.RoleUserEntity;
import com.lxd.sunso.product.service.RoleUserService;
import com.lxd.sunso.product.vo.UserVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lxd.sunso.product.entity.UserEntity;
import com.lxd.sunso.product.service.UserService;
import javax.servlet.http.HttpServletRequest;


/**
 * @author lxd
 * @email sunlightcs@gmail.com
 * @date 2020-11-06 17:43:43
 */
@RestController
@RequestMapping("product/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoderUtil bCryptPasswordEncoderUtil;

    @Autowired
    private RoleUserService roleUserService;

    private String tempPassword;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:user:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */

    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:user:info")
    public R findById(@PathVariable("id") Long id) {
        UserEntity user = userService.findById(id);
        user.setPassword("******");
        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:user:save")
    public R save(@RequestBody UserEntity user) {
        String encode = bCryptPasswordEncoderUtil.encode(user.getPassword());
        user.setPassword(encode);
        user.setCreatetime(DateUnit.dateFormat(new Date()));
        boolean flag = userService.saveUser(user);
        if (!flag) {
            return R.error(1, "用户名已存在");
        } else {
            return R.ok();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:user:update")
    public R update(@RequestBody UserEntity user) {
        userService.newUpdate(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:user:delete")
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));
        ArrayList<Long> userIds = new ArrayList<>();
        for (Long id : ids) {
            userIds.add(id);
        }
        roleUserService.remove(new QueryWrapper<RoleUserEntity>().in("UserId", userIds));
        return R.ok();
    }

    /**
     * 获取IP地址
     *
     * @param request
     * @return
     */
    @GetMapping("/testipaddress")
    public String queryAllByHour(HttpServletRequest request) {
        final String ip = IpUtil.getIpAddr(request);
        return ip;
    }

    /**
     * 根据条件查询
     */
    @RequestMapping("/search")
    //@RequiresPermissions("product:user:delete")
    public R search(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.search(params);
        return R.ok().put("page", page);
    }

    /**
     * 根据用户的名称得到顾客的名称
     */
    @RequestMapping("/findCustomerByUser/{username}")
    //@RequiresPermissions("product:user:delete")
    public R findCustomerByUse(@PathVariable("username") String username) {
        String customername = userService.findCustomerByUser(username);
        return R.ok().put("customername", customername);
    }

    /**
     * 校验码计算器
     */
    @RequestMapping("/check/{machineName}")
    //@RequiresPermissions("product:user:delete")
    public R search(@PathVariable String machineName) {
        String hexString = ConversionUtil.StringToHex(machineName);
        String s = CRC8Utils.CRC8_REMAXIM(hexString);
        String value = StringUtils.leftPad(s, 2, '0');
        return R.ok().put("value", value);
    }

    /**
     * 用户修改密码
     */
    @RequestMapping("/changePassword")
    public R changePassword(@RequestBody UserVo userVo) {
        boolean flag = userService.changePassword(userVo);
        if (flag) {
            return R.ok();
        } else {
            return R.error(1, "旧密码错误");
        }
    }
}
